package modle

import (
	"fmt"
	"pd/conf"
	"time"

	log "github.com/marmotedu/iam/pkg/log"
)

func NewDeviceInfos() *deviceInfos {
	return &deviceInfos{}
}

type deviceInfos struct {
	Id              int64      `json:"id" gorm:"column:id"`
	HardwareId      string     `json:"hardwareId" gorm:"column:hardware_id"`
	Guid            string     `json:"guid" gorm:"column:guid"`
	Mac             string     `json:"mac" gorm:"column:mac"`
	SerialNo        string     `json:"serialNo" gorm:"column:serial_no"`
	Guid2           string     `json:"guid2" gorm:"column:guid2"`
	Platform        string     `json:"platform" gorm:"column:platform"`
	Os              string     `json:"os" gorm:"column:os"`
	User            string     `json:"user" gorm:"column:user"`
	StaticUser      string     `json:"staticUser" gorm:"column:static_user"`
	ConfirmedUser   string     `json:"confirmedUser" gorm:"column:confirmed_user"`
	AccountName     string     `json:"accountName" gorm:"column:accountname"`
	DeviceName      string     `json:"deviceName" gorm:"column:device_name"`
	OtherName       string     `json:"otherName" gorm:"column:other_name"`
	OrgiSysDeviceId int64      `json:"orgiSysDeviceId" gorm:"column:orgi_sys_device_id"`
	Ip              string     `json:"ip" gorm:"column:ip"`
	Status          int64      `json:"status" gorm:"column:status"`
	CreateTime      *time.Time `json:"createTime" gorm:"column:create_time"`
	RecentUseTime   *time.Time `json:"recentUseTime" gorm:"column:recent_use_time"`
	DeviceType      int8       `json:"deviceType" gorm:"column:device_type"`
	App             string     `json:"app" gorm:"column:app"`
	GuidValid       int8       `json:"guidValid" gorm:"column:guidValid"`
	UseAge          string     `json:"useAge" gorm:"column:useage"`
	OriginUseAge    string     `json:"originUseAge" gorm:"column:origin_useage"`
	Imei            string     `json:"imei" gorm:"column:imei"`
	ReferenceId     int64      `json:"referenceId" gorm:"column:reference_id"`
	AccessType      string     `json:"accessType" gorm:"column:accesstype"`
	Remark          string     `json:"remark" gorm:"column:remark"`
	Confirmed       int        `json:"confirmed" gorm:"column:confirmed"`
	DeviceStyle     string     `json:"deviceStyle" gorm:"column:device_style"`
	NetType         int16      `json:"netType" gorm:"column:net_type"`
	AllUseAge       string     `json:"allUseAge" gorm:"column:all_useage"`
}

func (d *deviceInfos) TableName() string {
	return "device_info_ioa_v5"
}

// query by user and DeviceName
func (d *deviceInfos) GetDeviceInfo(user, DeviceName string) error {
	result := conf.C().Mysql2.GetBaseData().Where("user = ? AND device_name = ?", user, DeviceName).First(d)

	if err := result.Error; err != nil {
		log.Errorf("queryErr:", err.Error())
		return err
	} else if result.RowsAffected == 0 {
		return fmt.Errorf("queryErr: RowsAffected is 0")
	}

	return nil
}

func (d *deviceInfos) UpdateRecentUseTime() error {
	// 条件更新
	if d.HardwareId == "" {
		return fmt.Errorf("updateErr: HardwareId is nil")
	}

	result := conf.C().Mysql2.GetBaseData().Model(NewDeviceInfos()).Where("hardware_id = ?", d.HardwareId).
		Update("recent_use_time", time.Now().Format("2006-01-02 15:04:05"))

	if err := result.Error; err != nil {
		log.Errorf("updateErr:", err.Error())
		return err
	} else if result.RowsAffected == 0 {
		return fmt.Errorf("updateErr: RowsAffected is 0")
	}

	return nil
}
